<?php

class CartController extends Zend_Controller_Action {
	
	public function init() {
		/*
		 * Initialize action controller here
		 */
// 		$cart = new Zend_Session_Namespace ( 'change' );
// 		echo "<pre>";
// 		print_r($cart->getIterator());
// 		echo "</pre>";
// 		die;
	
	}
	
	public function indexAction() {//Hien thi thong tin ve gio hang
		$cart = new Zend_Session_Namespace ( 'cart' );//Khoi tao doi tuong session cart
		if (empty ( $cart->cart )) {// Chưa có gio hoa nao trong gio hang 
			echo '<script> alert ("Your cart is empty!"); </script>';//Alert cho nguoi dung biet
		} else {//Truong hop da co gio hoa trong gio hang
			//truyen xuong view
			$this->view->cart = $cart->cart;
		}
	}
	
	public function createorderAction(){//Tao gio hang
		$this->_helper->layout->disableLayout();//Ham` trung gian, xu ly -> ko can hien thi view/layout
		$this->_helper->viewRenderer->setNoRender(true);//Ko render view
		
		
		$id_sp = $this->_request->getParam ( 'id_sp' );//Lay id gio hoa tren url
		
		$cart = new Zend_Session_Namespace ( 'cart' );//Tao ra session gio hang
		if ($cart->cart [$id_sp] == null) {//Neu chua co gio hoa nay trong session
			$cart->cart [$id_sp] = 1;//Gan so luong la 1
		} else {
			$cart->cart [$id_sp] ++;//neu da co gio hoa trong session, tang so luong len 1
		}
		$this->_helper->redirector ( 'index' );//Quay ve trang cart(gio hang)
	}
	
	public function deleteoderAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$id_sp = $this->_request->getParam ( 'id_sp' );
		$cart = new Zend_Session_Namespace ( 'cart' );
		$cart_change = new Zend_Session_Namespace ( 'change' );
		foreach ($cart_change->change as $oneitem)//xóa yêu cầu thay đổi cho giỏ hoa này
		{
			if($oneitem['giohoa_id']==$id_sp)
			{
				unset($oneitem);
			}
		}
		unset($cart->cart[$id_sp]);
		$this->_helper->redirector ( 'index' );
	}
	
	public function deletecustomoderchangeAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$id_sp = $this->_request->getParam ( 'id_sp' );
		$cart_change = new Zend_Session_Namespace ( 'change' );
		foreach ($cart_change->change as $key=>$oneitem)//xóa yêu cầu thay đổi cho giỏ hoa này
		{
			if($oneitem['giohoa_id']==$id_sp)
			{
				unset($cart_change->change[$key]);
			}
		}
		$this->_helper->redirector ( 'index' );
	}
	
	public function updatedataAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);//Không sử dụng view
		$id_sp = $this->getRequest ()->getPost ('id');
		$soluong = $this->getRequest ()->getPost ('soluong');
		if($soluong > 1000) $soluong = 1000;
		if($soluong < 0) $soluong = 0;
		
		//Dựa vào id, lấy ra giá giỏ hoa
		$giohoa_table = new Application_Model_GioHoa();
		$giacu  = $giohoa_table->getGiaById($id_sp);//Lấy giá cũ
		$giamoi = (float)$giacu * (float)$soluong;//Tính giá mới
		
		//cập nhật lại session
		$cart = new Zend_Session_Namespace ( 'cart' );//Gọi đối tượng session cart; cart có dạng: id_sp=>soluong
		$cart->cart[$id_sp] = $soluong;//Cập nhật lại số lượng.
		$tongtien = $this->tongtienCart();
		$result = array();
		$result['giamoi'] = $giamoi;
		$result['tongtien'] = $tongtien;
		
		$json = Zend_Json::encode ( $result );//Chuyển sang kiểu json
		print_r ( $json );
	}
	
	public function tongtienCart(){
		$cart = new Zend_Session_Namespace ( 'cart' );
		$giohoa_table = new Application_Model_GioHoa();
		$tongtien = 0;
		foreach($cart->cart as $id_sp=>$soluong){
			$dongia = $giohoa_table->getGiaById($id_sp);
			$giatien = $dongia * $soluong;
			$tongtien += $giatien;
		}
		return $tongtien;
	}

	public function customoderAction(){
		$giohoa_id=$this->_request->getParam('id_sp');
		
		$ct_giohoa=new Application_Model_CTGioHoa();
		$ct_giohoa_table = new Application_Model_CTGioHoa();
		$hoa=new Application_Model_Hoa();
		$giohoa_table=new Application_Model_GioHoa();
		$list_tenhoa=array();
		$cart_change = new Zend_Session_Namespace ( 'change' );
				
		$session_found = false;
		foreach($cart_change->change as $motgiohang){
			if($motgiohang['giohoa_id'] == $giohoa_id )//Nếu giỏ hoa này đã bị modify
			{
				$session_found = true;
				break;
			}
		}
		if($session_found == true){//Load dữ liệu trong session
			$giohoasession=$giohoa_table->getGioHoa($giohoa_id);
			$x=$giohoasession['tiencongphuthu'];
			foreach ($cart_change->change as $motgiohang)
			{
				if($motgiohang['giohoa_id'] == $giohoa_id){
					$list_ct_giohoa=$ct_giohoa->getListCTGioHoa($motgiohang['giohoa_id']);
					$mothoa=$hoa->getHoa($motgiohang['hoa_id']);
					
					$list_tenhoa[$motgiohang['hoa_id']]['tenhoa']=$mothoa['tenhoa'];
					$list_tenhoa[$motgiohang['hoa_id']]['dongia']=$mothoa['dongia'] * $motgiohang['slhoa'];
					$list_tenhoa[$motgiohang['hoa_id']]['slhoa']= $motgiohang['slhoa'];
					
					$motgiohang['slhoa'];//SL trong sesion
					$xxx = $ct_giohoa_table->getSoluonghoa($motgiohang['hoa_id'], $motgiohang['giohoa_id']);//sl trong chi tiết giỏ hoa
					if(($xxx - $motgiohang['slhoa'])!=0)
						$list_tenhoa[$motgiohang['hoa_id']]['slthaydoi'] =  $xxx - $motgiohang['slhoa'];
					//Lấy giá = tương ứng với từng giỏ hoa(từng dòng)
					$x+=$list_tenhoa[$motgiohang['hoa_id']]['dongia'];
				}
			} /**/
			$this->view->abc=$x;
			
		}else{//Chưa bao giờ custom gio hang nay => Load dữ liệu trong database
	
			$list_ct_giohoa=$ct_giohoa->getListCTGioHoa($giohoa_id);
			
			foreach ($list_ct_giohoa as $value)
			{
				$mothoa=$hoa->getHoa($value['hoa_id']);
				$list_tenhoa[$value['hoa_id']]['tenhoa']=$mothoa['tenhoa'];
				$list_tenhoa[$value['hoa_id']]['dongia']=$mothoa['dongia'] * $value['slhoa'];
				$list_tenhoa[$value['hoa_id']]['slhoa']=$value['slhoa'];
				
				$new_giohang = array();
				$new_giohang['giohoa_id'] 	= $giohoa_id;
				$new_giohang['hoa_id'] 		= $value['hoa_id'];
				$new_giohang['slhoa'] 			= $value['slhoa'];
				$cart_change->change[]		= $new_giohang;
			}
		}		
		$gia_giohoa=$giohoa_table->getGiaById($giohoa_id);
		$this->view->list_tenhoa=$list_tenhoa;
		$this->view->dongia=$gia_giohoa;
		$this->view->list_ct_giohoa=$list_ct_giohoa;
		$this->view->giohoaid_customoder=$giohoa_id;
		
	}
	
	public function tinhtiencustomorderAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender(true);
		$giohoa_id=$this->_request->getParam('id_sp');
		$hoa_id = $this->_request->getParam('id');
		$sl=$this->_request->getParam('sl');
		$hoa_table=new Application_Model_Hoa();
		$hoa=$hoa_table->getHoa($hoa_id);
		$tongtien=(int)$hoa['dongia']*$sl;
		
		$cart_change = new Zend_Session_Namespace ( 'change' );
		foreach ($cart_change->change as &$oneitem)
			if((int)$oneitem['giohoa_id']==$giohoa_id)
				if($oneitem['hoa_id']==$hoa_id)
					$oneitem['slhoa']=$sl;
		$json = Zend_Json::encode ( $tongtien );//Chuyển sang kiểu json
		print_r ( $json );
	}
	
}

